package com.xiaonei.DAO;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;





//import com.sun.tools.javac.util.List;
import com.util.DBConnection;
import com.xiaonei.model.Post;

public class TopicDAOImpl implements ITopicDAO {

	@Override
	public ArrayList<String> toipcOfTypeList(String type) {
		ArrayList<String> list=new ArrayList<String>();
		Connection conn=DBConnection.getConnection();//得到数据库的连接�?		
		ResultSet rs=null;//结果�?		
		PreparedStatement pstmt=null;
		
		String sql="select content from topic where type='"+type+"'";
		try{
			pstmt=conn.prepareStatement(sql);
			rs=pstmt.executeQuery();
			while(rs.next()){
				list.add(rs.getString(1));
			}	
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			DBConnection.free(rs, conn, pstmt);
		}
		return list;
	}

	@Override
	public ArrayList<String> typeList() {
		ArrayList<String> list=new ArrayList<String>();
		Connection conn=DBConnection.getConnection();//得到数据库的连接�?
		ResultSet rs=null;//结果�?		
		PreparedStatement pstmt=null;
		
		String sql="select distinct type from topic";
		try{
			pstmt=conn.prepareStatement(sql);
			rs=pstmt.executeQuery();
			while(rs.next()){
				list.add(rs.getString(1));
			}	
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			DBConnection.free(rs, conn, pstmt);
		}
		return list;
	}

	@Override
	public int typeCount() {
		int count=100;
		   Connection conn=DBConnection.getConnection();//得到数据库的连接�?	
		   ResultSet rs=null;//结果�?			
		   PreparedStatement pstmt=null;
			String sql="select distinct type from topic";
			try{
				pstmt=conn.prepareStatement(sql);
				rs=pstmt.executeQuery();
				try{
					rs.last();//将数据库游标指向结果集的�?���?				
					}catch(SQLException e){
					e.printStackTrace();
				}
				try{
					count=rs.getRow();//得到结果集的总行数；
				}catch(SQLException e1){
					e1.printStackTrace();
				}
			}catch(Exception e){
				e.printStackTrace();
			}finally{
				DBConnection.free(rs, conn, pstmt);
			}
		   return count; 
	}

	public int topicCount() {
		int count=100;
		   Connection conn=DBConnection.getConnection();//得到数据库的连接�?	
		   ResultSet rs=null;//结果�?			
		   PreparedStatement pstmt=null;
			String sql="select distinct content from topic";
			try{
				pstmt=conn.prepareStatement(sql);
				rs=pstmt.executeQuery();
				try{
					rs.last();//将数据库游标指向结果集的�?���?				
					}catch(SQLException e){
					e.printStackTrace();
				}
				try{
					count=rs.getRow();//得到结果集的总行数；
				}catch(SQLException e1){
					e1.printStackTrace();
				}
			}catch(Exception e){
				e.printStackTrace();
			}finally{
				DBConnection.free(rs, conn, pstmt);
			}
		   return count; 
	}
	/**
	    *某个话题的帖子列�?	    *
	    *@param  topicContent 为话题内容；
	    *@return List型返回某个话题的帖子列表�?	    */	
	   public ArrayList<String> postOfTopicList(String content){
		   ArrayList<String> list=new ArrayList<String>();
		   return list;
	   }
	   
	   /**
	     *某个话题的类�?	     *
	     *@param  type为话题类�?	     *@return content为话题的名字
	     */	
	   public String getTypeOfTopic(String content){
		   String type=null;
			Connection conn=DBConnection.getConnection();//得到数据库的连接�?
			ResultSet rs=null;//结果�?			
			PreparedStatement pstmt=null;
			
			String sql="select type from topic where content='"+content+"'";
			try{
				pstmt=conn.prepareStatement(sql);
				rs=pstmt.executeQuery();
				while(rs.next()){
					type=rs.getString(1);
				}	
			}catch(Exception e){
				e.printStackTrace();
			}finally{
				DBConnection.free(rs, conn, pstmt);
			}
			return type;
	   }
	   
	   /**
	    *某个话题的id
	    *
	    *@param  content为话�?	    *@return int为话题id
	    */	
	  public int getIdOfTopic(String content){
		  int id=0;
		   Connection conn=DBConnection.getConnection();//得到数据库的连接�?
		   ResultSet rs=null;//结果�?			
		   PreparedStatement pstmt=null;
			String sql="select topic_num from topic where content='"+content+"'";
			try{
				pstmt=conn.prepareStatement(sql);
				rs=pstmt.executeQuery();
				while(rs.next()){
					id=rs.getInt(1);
				}	
			}catch(Exception e){
				e.printStackTrace();
			}finally{
				DBConnection.free(rs, conn, pstmt);
			}
			return id;
	  }
	  
	  /**
	    *某个话题的id
	    *
	    *@param  int为话题id    *@return content为话题；
	    */	
	  public String getContentOfTopic(int id){
		 String content=null;
		   Connection conn=DBConnection.getConnection();//得到数据库的连接�?
		   ResultSet rs=null;//结果�?			
		   PreparedStatement pstmt=null;
			String sql="select content from topic where topic_num='"+id+"'";
			try{
				pstmt=conn.prepareStatement(sql);
				rs=pstmt.executeQuery();
				while(rs.next()){
					content=rs.getString(1);
				}	
			}catch(Exception e){
				e.printStackTrace();
			}finally{
				DBConnection.free(rs, conn, pstmt);
			}
			return content;
	  }
	  
	  public boolean addTopic(String type,String content,String time){
		  boolean bool=false;
	  		Connection conn=DBConnection.getConnection();//得到数据库的连接�?		
	  		int crow;//结果�?		
	  		PreparedStatement pstmt=null;
			ResultSet rs=null;
			
			String sql="insert into topic(type,content,time) values('"+type+"','"+content+"','"+time+"')";
			try{
				pstmt=conn.prepareStatement(sql);
			    crow=pstmt.executeUpdate(sql);
			    if(crow==0) bool=false;
			    else if(crow==1) bool=true;
			}catch(Exception e){
				e.printStackTrace();
			}finally{
				DBConnection.free(rs, conn, pstmt);
			}
			return bool;
	  }
	  
	  
	  public int[] postList(ArrayList<String> idlist){
		   int[] list=null;
		   for(int i=0;i<20;i++){
			   integer[i]=new integer();
		   }
		   Connection conn=DBConnection.getConnection();//得到数据库的连接�?		
		   ResultSet rs=null;//结果�?		
		   PreparedStatement pstmt=null;
			try{
				
			for(int i=0;i<idlist.size();i++){
			String sql="select topic_num from post order by topic_num";
				pstmt=conn.prepareStatement(sql);
				rs=pstmt.executeQuery();
				while(rs.next()){
					Post e=new Post();
					e.setPost_num(rs.getInt("post_num"));
					e.setAccount(rs.getString("account"));
					e.setContent(rs.getString("content"));
					e.setImageUrl(rs.getString("imageurl"));
					e.setTheme(rs.getString("theme"));
					e.setTopic_num(rs.getInt("topic_num"));
					e.setTime(rs.getString("time"));
					postList.add(e);
				}	
			}
			
			}catch(Exception e){
				e.printStackTrace();
			}finally{
				DBConnection.free(rs, conn, pstmt);
			}
			return postList; 
		
	   }

	private int i() {
		// TODO Auto-generated method stub
		return 0;
	}
}
